package org.jwcloud.project.module.zx.baseManager.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.baseManager.service.repository.MatBaseManagerRepository;
import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

//@Api(value = "API - Controlle",
//description = "基础数据管理3个页面", tags = "baseManager")
//@RestController
//@Transactional
//@RequestMapping("/rest/mat/baseManager")
public class MatBaseManagerController extends BaseController{
	
	@Autowired
	public RptCommonRepository rptCommonRepository;
	
	@Autowired
	public MatBaseManagerRepository matBaseManagerRepository;
	
	@Autowired
    public RptUtService rptUtService;
	
	@RequestMapping("/test")
	public ModelAndView test(String param1, ModelAndView model) {
		model.setViewName("test/test.html");
		return model;
	}
	
	@RequestMapping("/view")
	public ModelAndView testView(String param1, ModelAndView model) {
		model.setViewName("test/BaseManager.html");
		return model;
	}
	
	@RequestMapping("/view2")
	public ModelAndView testView2(String param1, ModelAndView model) {
		model.setViewName("test/BaseManager2.html");
		return model;
	}
	
	@RequestMapping("/view3")
	public ModelAndView testView3(String param1, ModelAndView model) {
		model.setViewName("test/BaseManager3.html");
		return model;
	}
	
	@GetMapping("queryItemList")
    @ApiOperation(value = "查询要件信息配置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "item_name", value = "要件名称", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryItemList(
    		@RequestParam(required = false) Map<String,String> param
    		){
		return Resp.success(this.rptUtService.pageBuild(
				() -> this.matBaseManagerRepository.queryItemList(param), param));
    }
	
	@PostMapping("addItem")
    @ApiOperation(value = "添加要件", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "item_name", value = "要件名称", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "is_start", value = "启用", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "yfk_flag", value = "预付款", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "js_flag", value = "结算", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "dt_flag", value = "电梯", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "xf_flag", value = "消防", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "gsgdgq_flag", value = "供水、供电、供气", required = false, dataType = "string", paramType = "query"),
    	
    })
    public Resp addItem(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "item_name", "item_name"
				, "is_start", "启用"
				, "yfk_flag", "预付款"
				, "js_flag", "结算"
				, "dt_flag", "电梯"
				, "xf_flag", "消防"
				, "gsgdgq_flag", "供水、供电、供气");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return Resp.success(this.matBaseManagerRepository.addItem(param));
    }
	
	@PostMapping("updateItem")
    @ApiOperation(value = "修改要件", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "item_name", value = "要件名称", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "is_start", value = "", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "yfk_flag", value = "", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "js_flag", value = "", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "dt_flag", value = "", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "xf_flag", value = "", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "gsgdgq_flag", value = "", required = false, dataType = "string", paramType = "query"),
    	
    })
    public Resp updateItem(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id"
				, "item_name", "item_name"
				, "is_start", "is_start"
				, "yfk_flag", "yfk_flag"
				, "js_flag", "js_flag"
				, "dt_flag", "dt_flag"
				, "xf_flag", "xf_flag"
				, "gsgdgq_flag", "gsgdgq_flag");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return Resp.success(this.matBaseManagerRepository.updateItem(param));
    }
	
	
	@PostMapping("delItem")
    @ApiOperation(value = "删除要件", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "ids", value = "1;2;3", required = false, dataType = "string", paramType = "query"),
    	
    })
    public Resp delItem(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "ids", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		String ids = param.get("ids");
		String[] arr = ids.split(";");
		
		Map map = new HashMap();
		for(int i = 0; i < arr.length; i++) {
			map.put("id", arr[i]);
			this.matBaseManagerRepository.delItem(map);
		}
		return Resp.success(1);
    }
	
	@GetMapping("queryWorkList")
    @ApiOperation(value = "查询要件信息配置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "item_name", value = "要件名称", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryWorkList(
    		@RequestParam(required = false) Map<String,String> param
    		){
		return Resp.success(this.matBaseManagerRepository.queryWorkList(param));
    }
	
	@PostMapping("updateWork")
    @ApiOperation(value = "修改工作", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_idx", value = "工作序号", required = false, dataType = "string", paramType = "query"),
    })
    public Resp updateWork(
    		@RequestBody(required = false) Map param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return Resp.success(this.matBaseManagerRepository.updateWork(param));
    }
	
	
	@GetMapping("querySpPersonTree")
    @ApiOperation(value = "审批人树", notes = "")
    @ApiImplicitParams({
    })
    public Resp querySpPersonTree(
    		@RequestParam(required = false) Map<String,String> param
    		){
		List<Map<String,String>> list = this.matBaseManagerRepository.querySpPersonTree(param);
		if(list == null || list.size() == 0) {
			return Resp.error("系统未配置审批人");
		}
		List res = new ArrayList();
		Map tempMap = new LinkedHashMap();
		int cot = 1;
		for(Map<String,String> map: list) {
			if(tempMap.containsKey(map.get("sp_dept"))) {
				Map tempInner = new HashMap() {
					{
						//this.put("id", map.get("sp_person"));
						this.put("label", map.get("sp_person_name"));
						this.put("levelState", "1");
					}
				};
				tempInner.putAll(map);
				tempInner.put("id", cot++);
				((List)tempMap.get(map.get("sp_dept"))).add(tempInner);
			}else {
				List innerList = new ArrayList();
				Map tempInner = new HashMap() {
					{
						//this.put("id", map.get("sp_person"));
						this.put("label", map.get("sp_person_name"));
						this.put("levelState", "1");
					}
				};
				tempInner.putAll(map);
				tempInner.put("id", cot++);
				innerList.add(tempInner);
				tempMap.put(map.get("sp_dept"), innerList);
				tempMap.put(map.get("sp_dept") + "_", map);
			}
		}
		Iterator it = tempMap.keySet().iterator();
		while(it.hasNext()) {
			String key = (String) it.next();
			if(key.endsWith("_")) {
				continue;
			}
			Map resMap = new LinkedHashMap();
//			resMap.put("id", ((Map)tempMap.get(key + "_")).get("sp_dept"));
			resMap.put("id", cot++);
			resMap.put("sp_dept", ((Map)tempMap.get(key + "_")).get("sp_dept"));
			resMap.put("label", ((Map)tempMap.get(key + "_")).get("sp_dept_name"));
			resMap.put("levelState", "2");
			resMap.put("children", tempMap.get(key));
			res.add(resMap);
		}
		return Resp.success(res);
    }
	
	@GetMapping("querySpPersonList")
    @ApiOperation(value = "财务审批人设置", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_person_name", value = "要件名称", required = false, dataType = "string", paramType = "query"),
    })
    public Resp querySpPersonList(
    		@RequestParam(required = false) Map<String,String> param
    		){
		return Resp.success(this.matBaseManagerRepository.querySpPersonList(param));
    }
	
	@PostMapping("addSpPerson")
    @ApiOperation(value = "增加审批人", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "person_id", value = "审批人id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "person_name", value = "审批人名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "person_dept", value = "审批人部门", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "person_dept_name", value = "审批人部门", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_level", value = "审批层级", required = false, dataType = "string", paramType = "query"),
    })
    public Resp addSpPerson(
    		@RequestBody(required = false) Map param
    		){
		String flag = FmtUtil.nullRes(param
				, "person_id", "审批人id"
				, "person_name", "审批人名"
				, "person_dept", "审批人部门"
				, "person_dept_name", "审批人部门"
				, "sp_level", "审批层级");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		Map dao = new HashMap();
		String person_id_ = (String) param.get("person_id");
		String person_name_ = (String) param.get("person_name");
		String person_dept_ = (String) param.get("person_dept");
		String person_dept_name_ = (String) param.get("person_dept_name");
		String sp_level_ = (String) param.get("sp_level");
		
		String[] person_ids = person_id_.split(";");
		String[] person_names = person_name_.split(";");
		String[] person_depts = person_dept_.split(";");
		String[] person_dept_names = person_dept_name_.split(";");
		String[] sp_levels = sp_level_.split(";");
		
		for(int i = 0; i < person_ids.length; i++) {
			dao.put("person_id", person_ids[i]);
			dao.put("person_name", person_names[i]);
			dao.put("person_dept", person_depts[i]);
			dao.put("person_dept_name", person_dept_names[i]);
			dao.put("sp_level", sp_levels[i]);
			this.matBaseManagerRepository.addSpPerson(dao);
		}
		
		return Resp.success(1);
    }
	
	@PostMapping("updateSpPerson")
    @ApiOperation(value = "修改审批人", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "person_id", value = "person_id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "person_name", value = "person_name", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "person_dept", value = "person_dept", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "person_dept_name", value = "person_dept_name", required = false, dataType = "string", paramType = "query"),
    })
    public Resp updateSpPerson(
    		@RequestBody(required = false) Map param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return Resp.success(this.matBaseManagerRepository.updateSpPerson(param));
    }
	
	@PostMapping("delSpPerson")
    @ApiOperation(value = "删除审批人", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp delSpPerson(
    		@RequestBody(required = false) Map param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return Resp.success(this.matBaseManagerRepository.delSpPerson(param));
    }
	
	@PostMapping("updateSpPersonIdx")
    @ApiOperation(value = "修改审批人顺序", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "qian_id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "hou_id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "qian_sp_level", value = "sp_level", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "hou_sp_level", value = "sp_level", required = false, dataType = "string", paramType = "query"),
    })
    public Resp updateSpPersonIdx(
    		@RequestBody(required = false) Map param
    		){
		String flag = FmtUtil.nullRes(param
				, "qian_id", "qian_id"
				, "hou_id", "hou_id"
				, "qian_sp_level", "qian_sp_level"
				, "hou_sp_level", "hou_sp_level");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		Map dao = new HashMap();
		dao.put("id", param.get("qian_id"));
		dao.put("sp_level", param.get("hou_sp_level"));
		this.matBaseManagerRepository.updateSpPerson(dao);
		dao.put("id", param.get("hou_id"));
		dao.put("sp_level", param.get("qian_sp_level"));
		this.matBaseManagerRepository.updateSpPerson(dao);
		
		return Resp.success(2);
    }
	
}